// EtyMain.ts
// entity

import { PkgUser } from "./PkgUser";

//---------------------------------------------------------------------//

export class EtyMain {
	//---------------------------------------------------------------------//

	public static main(
	):void {
		console.log("A test for converting proto to json.");
		let oRpd:PkgUser.PhtList_Respond = new PkgUser.PhtList_Respond();
		oRpd.code = 0x3001;
		oRpd.command = "Usr_Pht_List";
		oRpd.result = "0";
		oRpd.message = "成功";
		let oItm:PkgUser.PhtList_Item = new PkgUser.PhtList_Item();
		oItm.pht_idntifr = "1";
		oItm.pht_status = "1";
		oItm.url_finalztion = "https://www.baidu.com/1.jpg";
		oRpd.list.push(oItm);
		console.log("PhtList_Respond is: ", JSON.stringify(oRpd.toJSON()));

		let oBfr:Object = PkgUser.PhtList_Respond.encode(oRpd).finish();
		let nBfr:Number = (oBfr as Uint8Array).length;
		oRpd = null;

		let oPkg:PkgUser.MsgCommon_Respond = null;
		try {
			oPkg = PkgUser.MsgCommon_Respond.decode(oBfr as Buffer, 5);
		} catch (e) {
			console.log("Respond is error: " + e.message);
		}

		if (oPkg != null) {
			switch (oPkg.code) {
				case 0x3001: {
					try {
						oRpd = PkgUser.PhtList_Respond.decode(oBfr as Buffer, nBfr as number);
					} catch (e) {
						console.log("Respond is error: " + e.message);
					}
				}
				break;
			}
			oPkg = null;
		}
		oBfr = null;
		oRpd = null;

		let sTxt:String = "{\"code\":12289,\"command\":\"Usr_Pht_List\",\"result\":\"0\",\"message\":\"成功\",\"list\":[{\"pht_idntifr\":\"1\",\"pht_status\":\"1\",\"url_finalztion\":\"https://www.baidu.com/1.jpg\"}]}";
		let jDat:{} = JSON.parse(sTxt as string);

		oRpd = PkgUser.PhtList_Respond.fromObject(jDat);
		oRpd = null;
	}
}

EtyMain.main();